from dao.ReferDao import ReferDao
from StatusCode import StatusCode
import pandas as pd
from util import CommonHelper


class ReferService:
    def getReferList(self, json_data):
        return ReferDao().getReferList()

    def getReferInfo(self, json_data):
        dao = ReferDao()
        print(json_data)
        referId = int(json_data.get("referId", -1))
        flag = dao.checkReferExistByReferId(referId)
        if not flag:
            return StatusCode.RESPONSE_FAIL
        df = dao.getReferInfo(referId).to_dict(orient="records")[0]
        return df

    def addRefer(self, json_data):
        try:
            dao = ReferDao()
            patientId = json_data.get("patient_id", "-1")
            createDate = CommonHelper.parseUTCTime(
                json_data.get("create_date", "").strip()
            )
            if dao.checkReferExist(patientId, createDate):
                return StatusCode.RESPONSE_EXIST
            outDeptId = json_data.get("out_dept", -1)
            inDeptId = json_data.get("in_dept", -1)
            doctorId = json_data.get("doctor", -1)
            diagnosis = json_data.get("diagnosis", "-")
            according = json_data.get("according", "-")
            status = 0
            refer = {
                "out_dept": outDeptId,
                "in_dept": inDeptId,
                "doctor_id": doctorId,
                "status": status,
                "create_date": createDate,
                "patient_id": patientId,
                "diagnosis": diagnosis,
                "according": according,
            }
            df = pd.DataFrame(data=[refer], columns=refer.keys())
            dao.addRefers(df)
            return StatusCode.RESPONSE_SUCCESS
        except Exception as e:
            print(e)
            return StatusCode.RESPONSE_FAIL

    def updateRefer(self, json_data):
        try:
            dao = ReferDao()
            referId = json_data.get("id", -1)
            flag = dao.checkReferExistByReferId(referId)
            if not flag:
                return StatusCode.RESPONSE_FAIL
            patientId = int(json_data.get("patient_id", "-1"))
            createDate = CommonHelper.parseUTCTime(
                json_data.get("create_date", "").strip()
            )
            outDeptId = json_data.get("out_dept", -1)
            inDeptId = json_data.get("in_dept", -1)
            doctorId = json_data.get("doctor_id", -1)
            diagnosis = json_data.get("diagnosis", "-")
            according = json_data.get("according", "-")
            dao.updateRefer(
                referId,
                patientId,
                outDeptId,
                inDeptId,
                doctorId,
                createDate,
                diagnosis,
                according,
            )
            return StatusCode.RESPONSE_SUCCESS
        except Exception as e:
            print(e)
            return StatusCode.RESPONSE_FAIL
        return ReferDao().updateRefer(json_data)

    def delRefer(self, json_data):
        try:
            referId = json_data.get("referId", -1)
            if referId == -1:
                return StatusCode.RESPONSE_FAIL
            ReferDao().delRefer(referId)
            return StatusCode.RESPONSE_SUCCESS
        except Exception as e:
            print(e)
            return StatusCode.RESPONSE_FAIL

    def checkTodoRefer(self, json_data):
        print(json_data)
        deptId = json_data.get("deptId", -1)
        data = ReferDao().checkTodoRefer(deptId)
        return data

    def confirmRefer(self, json_data):
        referId = json_data.get("referId", -1)
        if referId == -1:
            return StatusCode.RESPONSE_FAIL
        ReferDao().confirmRefer(referId)
        return StatusCode.RESPONSE_SUCCESS
